﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DingHeng.TransportationManagement.Business;
using DingHeng.TransportationManagement.Entity.Constant;
using DingHeng.TransportationManagement.Entity;
using DingHeng.TransportationManagement.WebCommon;
using DingHeng.TransportationManagement.WebCommon.BaseUI;

namespace DingHeng.TransportationManagement.WebSite.Biz
{
    public partial class VehicleDispatchFormView : BasePage
    {
        private Guid? VehicleDispatchFormID
        {
            get
            {
                Guid id;
                if (string.IsNullOrEmpty(Request.QueryString.Get("VehicleDispatchFormID")) || Guid.TryParse(Request.QueryString.Get("VehicleDispatchFormID"), out id) == false)
                {
                    return null;
                }
                else
                {
                    return id;
                }
            }
        }
        private void initPage(DingHeng.TransportationManagement.Entity.VehicleDispatchForm VehicleDispatchFormInstance)
        {
            if (VehicleDispatchFormInstance.VehicleID.HasValue)
            {
                VehicleInfo info = new VehicleBiz().LoadByInfoID(VehicleDispatchFormInstance.VehicleID.Value);
                txtVehicle.Text = info.LicensePlateNo;
                txtVehicleOwner.Text = info.OwnerName;
                txtVehicleCode.Text = info.Code;
            }
            txtCode.Text = VehicleDispatchFormInstance.Code;
            txtDriverPhoneNo.Text = VehicleDispatchFormInstance.DriverContactNo;
            txtDriver.Text = VehicleDispatchFormInstance.Driver;
            txtOwnerContactNo.Text = VehicleDispatchFormInstance.VehicleOwnerContactNo;
            txtProjectName.Text = VehicleDispatchFormInstance.ProjectName;            
            if (VehicleDispatchFormInstance.PrincipalID.HasValue)
            {
                User user = new UserBiz().LoadById(VehicleDispatchFormInstance.PrincipalID.Value);
                if(user!=null)
                {
                    this.txtPricipal.Text = user.Name;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (VehicleDispatchFormID.HasValue)
                {
                    lblHomeTitle.Text = "派车单详情";
                    DingHeng.TransportationManagement.Entity.VehicleDispatchForm VehicleDispatchFormInstance
                         = new VehicleDispatchFormBiz().LoadById(VehicleDispatchFormID.Value);
                    if (VehicleDispatchFormInstance != null
                        && VehicleDispatchFormInstance.Status.HasValue)
                    {
                        if (VehicleDispatchFormInstance.Status.Value == Guid.Parse(StringConstant.StatusDispatched))
                        {
                            this.btnGenerate.Visible = true;
                            //this.btnPrint.Visible = true;
                        }
                        else if (VehicleDispatchFormInstance.Status.Value == Guid.Parse(StringConstant.StatusCheckIn))
                        {
                            this.btnPrint.Visible = true;

                        }
                    }
                    //
                    initPage(VehicleDispatchFormInstance);
                }               

            }
        }

        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            VehicleDispatchFormBiz vehicleDispatchFormBiz = new VehicleDispatchFormBiz();
            TransportationForm transportationForm = vehicleDispatchFormBiz.GenerateTransportationForm(this.VehicleDispatchFormID.Value,WebContext.Current.CurrentUser.Name);
            if (transportationForm != null)
            {
                Response.Redirect(string.Format("TransportationFormSubmit.aspx?TransportationFormID={0}", transportationForm.ID));
            }
        }

    }
}